﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace GameCore
{
    public  class TimerManager:MonoSingleton<TimerManager>
    {
        public  List<TimerEvent> timerList = new List<TimerEvent>();
        public  TimerEvent timerEventDefault=new TimerEvent();

        private void Update()
        {
            this.Update();
        }

        public  void OnUpdate()
        {    
            for (int i = 0; i < timerList.Count; i++)
            {
                TimerEvent e = timerList[i];
                e.Update();

                if (e.isDone)
                {
                    e.CompleteTimer();

                    if (e.isDone)
                    {
                        timerList.Remove(e);
                        i--;
                    }
                }
            }
        }
        public  bool GetIsExistTimer(string timerName)
        {
            return timerList.Find(e => e.timerName == timerName) != null;
        }
        public  TimerEvent GetTimer(string timerName)
        {
            var timer = timerList.Find(e => e.timerName == timerName);
            if (timer != null)
                return timer;

            throw new System.Exception("Get Timer  Exception not find ->" + timerName + "<-");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="spaceTime"></param>
        /// <param name="loopCount">重复调用的次数</param>
        /// <param name="callBackArgs">回调函数</param>
        /// <param name="args">回调函数的参数</param>
        /// <param name="timerName">Timer的名字</param>
        /// <param name="isIgnoreTimeScale">是否忽略时间缩放</param>
        /// <returns></returns>
        public  TimerEvent AddTimer<T>(float spaceTime, int loopCount, Action<T> callBackArgs,T args,string timerName=null, bool isIgnoreTimeScale = false) 
        {
            TimerEvent<T> timer = new TimerEvent<T>();
            timer.timerName = timerName ?? timer.GetHashCode().ToString();
            timer.curTimer = 0;
            timer.timerSpace = spaceTime;
            timer.args = args;
            timer.callBackArgs = callBackArgs;
            timer.isIgnoreTimeScale = isIgnoreTimeScale;
            timer.loopCount = loopCount;

            timerList.Add(timer);
            return timer;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="spaceTime">间隔时间</param>
        /// <param name="loopCount">重复调用的次数</param>
        /// <param name="callBack">回调函数</param>
        /// <param name="timerName">Timer的名字</param>
        /// <param name="isIgnoreTimeScale">是否忽略时间缩放</param>
        /// <returns></returns>
        public  TimerEvent AddTimer(float spaceTime, int loopCount, Action callBack, string timerName = null, bool isIgnoreTimeScale=false)
        {
            TimerEvent timer = new TimerEvent();

            timer.timerName = timerName ?? timer.GetHashCode().ToString();
            timer.curTimer = 0;
            timer.timerSpace = spaceTime;
            timer.callBack = callBack;
            timer.isIgnoreTimeScale = isIgnoreTimeScale;
            timer.loopCount = loopCount;

            timerList.Add(timer);
            return timer;
        }

        public  void ResetTimer(TimerEvent timer)
        {
            if (timerList.Contains(timer))
            {
                timer.ResetTimer();
            }
            else
            {
                Debug.LogError("Timer ResetTimer error: dont exist timer " + timer);
            }
        }
        public  void ResetTimer(string timerName)
        {
            var timer = timerList.Find(o=>o.timerName==timerName);
            if (timer!=null)
                ResetTimer(timer);          
        }

        public  void DestroyTimer(TimerEvent timer, bool isCallBack = false)
        { 
            if (timerList.Contains(timer))
            {
                if (isCallBack)               
                    timer.OnTimerCallBack();

                timerList.Remove(timer);
            }
            else
            {
                Debug.LogError("Timer DestroyTimer error: dont exist timer " + timer);
            }
        }
        public  void DestroyTimer(string timerName, bool isCallBack = false)
        {
            var timer = timerList.Find(o => o.timerName == timerName);
            DestroyTimer(timer, isCallBack);
        }
        public  void DestroyAllTimer(bool isCallBack = false)
        {
            for (int i = 0; i < timerList.Count-1; i++)
            {
                if (isCallBack)                
                    timerList[i].OnTimerCallBack();
                
            }

            timerList.Clear();
        }
    }
}
